#include "downloadthread.h"
#include "downloadmanager.h"

YDDownloadThread::YDDownloadThread(QObject *parent) :
    QThread(parent) {
    this->mgr = new YDDownloadManager(this);
    connect(this->mgr, &YDDownloadManager::sigStarted, this, &YDDownloadThread::started);
    connect(this->mgr, &YDDownloadManager::sigStoped, this, &YDDownloadThread::stoped);
    connect(this->mgr, &YDDownloadManager::sigErrorOccured, this, &YDDownloadThread::errorOccured);
    connect(this->mgr, &YDDownloadManager::sigFinished, this, &YDDownloadThread::finished);
    connect(this->mgr, &YDDownloadManager::sigProgressChanged, this, &YDDownloadThread::progressChanged);
    connect(this->mgr, &YDDownloadManager::sigDuplicate, this, &YDDownloadThread::duplicate);
    connect(this, &YDDownloadThread::sigStartDownload, this->mgr, &YDDownloadManager::startDownload);
    connect(this, &YDDownloadThread::sigStopDownload, this->mgr, &YDDownloadManager::stopDownload);
}

YDDownloadThread::~YDDownloadThread() {
}

void YDDownloadThread::run() {
    this->mgr->moveToThread(this);
    this->YDDownloadThread::run();
}

void YDDownloadThread::started(const QString& url) {
    emit this->sigStarted(url);
}

void YDDownloadThread::stoped(const QString& url) {
    emit this->sigStoped(url);
}

void YDDownloadThread::errorOccured(const QString& url, int code) {
    emit this->sigErrorOccured(url, code);
}

void YDDownloadThread::finished(const QString& url) {
    emit this->sigFinished(url);
}

void YDDownloadThread::progressChanged(const QString& url, qint64 bytesReceived, qint64 bytesTotal) {
    emit this->sigProgressChanged(url, bytesReceived, bytesTotal);
}

void YDDownloadThread::duplicate(const QString& url) {
    emit this->sigDuplicate(url);
}

void YDDownloadThread::startDownload(const QString& url, const QString& filePath, bool append) {
    emit this->sigStartDownload(url, filePath, append);
}

void YDDownloadThread::stopDownload(const QString& url) {
    emit this->sigStopDownload(url);
}
